﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;

namespace RssBar
{
    /// <summary>
    /// Interaction logic for ucOptionsProxy.xaml
    /// </summary>
    public partial class ucOptionsProxy : UserControl, IOptionsContentPage
    {
        public ucOptionsProxy()
        {
            InitializeComponent();

            try
            {
                this.IsEnabled = false;

                //load proxy settings
                WebProxy proxy = Configuration.Proxy as WebProxy;
                NetworkCredential credentials = proxy.Credentials as NetworkCredential;

                string proxyAddr = string.Empty;
                int proxyPort = -1;

                tbUsername.Text = credentials.UserName;
                tbPass.Text = credentials.Password;
                if (proxy.Address != null)
                {
                    proxyAddr = proxy.Address.Host;
                    proxyPort = proxy.Address.Port;
                }
                tbAddress.Text = proxyAddr;
                tbPort.Text = proxyPort.ToString();

                cbDefaultUserCredentials.IsChecked = proxy.UseDefaultCredentials;

            }
            catch (Exception e)
            {
                IApplication.Logger.Write(IApplication.LoggerEventType.Warning, e.Message, "[ucOptionsProxy.ucOptionsProxy] Nastąpił błąd podczas tworzenia karty proxy!");
            }
        }

        public string TitleText
        {
            get { return "Proxy"; }
        }

        private void cbDefaultUserCredentials_Checked(object sender, RoutedEventArgs e)
        {
            tbPass.IsEnabled = false;
            tbUsername.IsEnabled = false;
        }

        private void cbDefaultUserCredentials_Unchecked(object sender, RoutedEventArgs e)
        {
            tbPass.IsEnabled = true;
            tbUsername.IsEnabled = true;
        }

        public bool Apply()
        {
            try
            {
                //proxy
                WebProxy proxy = new WebProxy();

                string proxyAddr = tbAddress.Text;
                int proxyPort = (!string.IsNullOrEmpty(tbPort.Text) ? int.Parse(tbPort.Text) : -1);
                if (!string.IsNullOrEmpty(proxyAddr) && proxyPort > -1)
                    proxy.Address = new Uri(string.Format("{0}:{1}", proxyAddr, proxyPort));

                proxy.Credentials = new NetworkCredential(tbUsername.Text, tbPass.Text);

                proxy.UseDefaultCredentials = cbDefaultUserCredentials.IsChecked.Value;

                Configuration.Proxy = proxy;
            }
            catch (Exception e)
            {
                IApplication.Logger.Write(IApplication.LoggerEventType.Warning, e.Message, "[ucOptionsProxy.Apply] Nastąpił błąd podczas zapisywania ustawień");
            }

            return true;
        }
    }
}
